#include <iostream>
#include <stdio.h>

/*
 * 多继承有函数覆盖的特点
 */

class Base1
{
	public:
		virtual void function_1()
		{
			printf("Base1 function_1...\n");
		}

		virtual void function_2()
		{
			printf("Base1 function_2...\n");
		}
	
};

class Base2
{
	public:
		virtual void function_3()
		{
			printf("Base2 function_3...\n");
		}

		virtual void function_4()
		{
			printf("Base2 function_4...\n");
		}
	
};


class Sub :  Base1,  Base2
{
	public:
		virtual void function_1()
		{
			printf("Sub function_1...\n");
		}

		virtual void function_3()
		{
			printf("Sub function_3...\n");
		}

		virtual void function_5()
		{
			printf("Sub function_5...\n");
		}
};

int main(int argc, char ** argv)
{
	Sub sub;

	printf("size = %ld\n", sizeof(class Sub));	// 16。 直接继承两个父类，所以有两个虚函数表

	printf("sub address is 0x%lx.\n", (long)&sub);		// this 指针，是堆栈里面的地址。虚表的地址不是this指针，是this指针的前四个字节。

	/* 对象的前4字节是第一个Base1的虚表 */
	printf("sub virtual table address is:0x%lx\n", *(long*)&sub);   
	printf("sub virtual table address is:%p\n", (void*)*(long*)&sub); 

	typedef void(*pfunction)(void);
	
	pfunction pf;

	for (int i = 0; i < 4; i++) {
		long temp = *((long*)(*(long*)&sub) + i);	
		if (temp == 0) {
			break;
		}

		pf = (pfunction)temp;
		pf();
	}

	/* 对象的第二个4字节是Base2的虚表 */
	printf("sub virtual table address is:0x%lx\n", *(long*)((long)&sub+8));  
	pfunction pf2;

	for (int k = 0; k < 2; k++) {
		long temp = *((long*)(*(long*)((long)&sub+8)) + k);
		pf2 = (pfunction)temp;
		pf2();
	}

	return 0;
}

/*
Sub function_1
Base1 function_2
Sub function_5

Sub address
Sub function_3
Base2 function_4
 */

#if 0

1.覆盖的是哪个在哪个虚表里；
2.。
3.。



#endif
